package com.calque;

import java.util.ArrayList;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.widget.Toast;

import com.commun.AjouterMosque;
import com.commun.Param;
import com.commun.Utils;
import com.commun.Utils.CalqueEnum;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.object.Mosque;
import com.object.OverlayItemCustom;

public class CalqueSitesClientTempMosqueAjouter extends
		ItemizedOverlay<OverlayItemCustom> {

	/* Declration */
	private List<OverlayItemCustom> items = new ArrayList<OverlayItemCustom>();

	private Drawable marker = null;
	private Context context = null;
	OverlayItemCustom overlayItem;
	MapView mapView;
	public static Toast myToast;
	private Handler mHandler = new Handler();
	private Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			Utils.DisparitonLayoutTempItemTaost(context);
		}
	};
	private static CalqueSitesClientTempMosqueAjouter instance;

	public static CalqueSitesClientTempMosqueAjouter getInstance() {

		return instance;
	}

	public static void InitialisationInsatance(Drawable marker,
			Context context, MapView mapView) {

		instance = new CalqueSitesClientTempMosqueAjouter(marker, context,
				mapView);
	}

	private CalqueSitesClientTempMosqueAjouter(Drawable marker,
			Context context, MapView mapView) {
		super(marker);
		this.marker = marker;
		this.context = context;
		this.mapView = mapView;

		populate();

	}

	public MapView getMapView() {
		return mapView;
	}

	public void setMapView(MapView mapView) {
		this.mapView = mapView;
	}

	public void AddCalque(GeoPoint geoPoint) {

		OverlayItemCustom item = new OverlayItemCustom(geoPoint, "", "");
		items.add(item);
		populate();

	}

	public List<OverlayItemCustom> getItems() {
		return items;
	}

	public void AddCalque(GeoPoint geoPoint, Mosque mosque) {
		OverlayItemCustom item = new OverlayItemCustom(geoPoint, mosque
				.getInfo(), mosque.getNom());
		item.setMosquai(mosque);
		removeLastItem();
		items.add(item);
		populate();

	}

	public void Renitialise() {
		items = new ArrayList<OverlayItemCustom>();
	}

	public void remove(int ID) {
		if (size() == 1)
			Renitialise();
		else {
			items.remove(ID);
			populate();
		}

	}

	public void removeLastItem() {
		items.remove(size() - 1);
		populate();
	}

	@Override
	protected OverlayItemCustom createItem(int i) {
		return (items.get(i));
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, false);
		boundCenterBottom(marker);

		if (AjouterMosque.isTaptagOnMapDetected
				&& Utils.geoPointsAreSame(AjouterMosque.tapMosqueGeoPoint,
						mapView.getMapCenter())) {
			animationLaunched = false;
			AjouterMosque.isTaptagOnMapDetected = false;
			AjouterMosque.ShowDialogSaisiInfo();

		}

		if (!items.isEmpty()
				&& !AjouterMosque.isTagButtonActivate
				&& !AjouterMosque.isTaptagOnMapDetected
				&& animationLaunched
				&& Utils.geoPointsAreSame(items.get(nbTaped).getPoint(),
						mapView.getMapCenter())) {

			String text = items.get(nbTaped).getTitle() + " "
					+ items.get(nbTaped).getSnippet();

			AjouterMosque.MyCustomisedTempItemToast(context, text, nbTaped);

			mHandler.removeCallbacks(mUpdateTimeTask);
			mHandler.postDelayed(mUpdateTimeTask, Param.LONG_DELAY);

			animationLaunched = false;
			Utils.calqueEnum = CalqueEnum.TEMP_ITEMS;
		}

	}

	public boolean animationLaunched = false;

	private int nbTaped;

	@Override
	protected boolean onTap(int i) {

		if (!items.isEmpty()) {
			mapView.getController().animateTo(items.get(i).getPoint());
			animationLaunched = true;
			nbTaped = i;
		}

		return (true);
	}

	@Override
	public int size() {

		return (items.size());
	}

	public void Populate() {
		populate();
	}

	public OverlayItemCustom GetItem(int ID) {
		return items.get(ID);
	}

	public int GetIdLastOne() {
		return nbTaped;
	}
}